#ifndef __CROSSFIRE_MATH_BOUNDSPHERE
#define __CROSSFIRE_MATH_BOUNDSPHERE

namespace Math
{

class CCF_BoundingSphere
{
private:
	D3DXVECTOR3	center;
	float		radius;
	float		radiusSq;
public:
	CCF_BoundingSphere ( const D3DXVECTOR3& c, float r ) : center ( c ), radius ( r ) 
	{
		radiusSq = r * r;
	}
	const D3DXVECTOR3&	getCenter () const
	{
		return center;
	}

	float	getRadius () const
	{
		return radius;
	}

	void	addPoint ( const D3DXVECTOR3& p )
	{
		float	rSq = D3DXVec3Length(&(p-center));
		rSq *= rSq;

		if ( rSq > radiusSq )
		{
			radiusSq = rSq;
			radius   = (float) sqrt ( radiusSq );
		}
	}

	bool contains ( const D3DXVECTOR3& p ) const
	{
		float lenSq = D3DXVec3Length(&(p-center));
		lenSq *= lenSq;

		return lenSq <= radiusSq;
	}

	bool	contains ( const CCF_BoundingSphere& s ) const
	{
		return D3DXVec3Length(&(s.center - center)) + s.radius <= radius;
	}

	bool	intersects ( const CCF_BoundingSphere& s ) const
	{
		float lenSq = D3DXVec3Length(&(center - s.center));
		lenSq *= lenSq;

		return lenSq <= (radius + s.radius) * (radius + s.radius);
	}
};

};

#endif